package com.sx.pipeline.ui.service

import android.text.Editable
import android.text.TextWatcher
import android.view.View
import androidx.annotation.NonNull
import androidx.recyclerview.widget.GridLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.scwang.smart.refresh.layout.api.RefreshLayout
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener
import com.sx.mvvmlibs.bus.RxBus
import com.sx.mvvmlibs.view.SpaceItemDecoration2
import com.sx.pipeline.BR
import com.sx.pipeline.R
import com.sx.pipeline.adapter.LedgerDeviceAdapter
import com.sx.pipeline.adapter.ServiceDeviceAdapter
import com.sx.pipeline.app.base.CYDataBindingBaseActivity
import com.sx.pipeline.app.event.DeviceEvent
import com.sx.pipeline.arouth.RouteManager
import com.sx.pipeline.arouth.RoutePath
import com.sx.pipeline.databinding.ActMyServiceBinding
import com.sx.pipeline.repos.http.req.ServiceAddDTO
import io.reactivex.rxjava3.schedulers.Schedulers


/**
 * @Author: YY
 * @Date: 2022/4/5
 * @Description: 我的服务
 */
@Route(path = RoutePath.Service.SERVICE_LIST)
class MyServiceActivity :
    CYDataBindingBaseActivity<ActMyServiceBinding,ServiceVM>(R.layout.act_my_service), View.OnClickListener {
    private lateinit var mAdapter: ServiceDeviceAdapter
    private var key=""

    override fun initData() {
        mBinding.setVariable(BR.onClick, this)
        mBinding.titleBar.leftView.setOnClickListener {
            finish()
        }
        mBinding.titleBar.rightView.setOnClickListener {
            RouteManager.Service.intentToServiceAdd(1,0)
        }

        //请求设备列表
        mViewModel.getPmsServiceList(key, limit = 10)

        RxBus.getDefault().toObservable(DeviceEvent::class.java)
            .subscribeOn(Schedulers.io())
            .subscribe {
                //刷新设备列表
                mViewModel.page=1
                mViewModel.getPmsServiceList("", limit = 10)
            }
    }

    private fun initRv() {
        if (!this::mAdapter.isInitialized) {
            mAdapter = ServiceDeviceAdapter()
            mBinding.recyclerView.layoutManager =
                GridLayoutManager(this, 2)
            mBinding.recyclerView.addItemDecoration(
                SpaceItemDecoration2(20)
            )
        }
        mAdapter.setOnItemClickListener { adapter, view, position ->
            var info = adapter.data[position] as ServiceAddDTO
            info.id?.let { RouteManager.Service.intentToServiceAdd(2, it) }
        }
        mBinding.recyclerView.adapter = mAdapter
    }


    override fun initViewObservable() {
        super.initViewObservable()

        mViewModel.deviceListEvent.observe(this) {
            initRv()
            val taskList = it as List<ServiceAddDTO>
            if (mViewModel.page == 1) {
                if (taskList.isNotEmpty()) {
                    mAdapter?.data = taskList as MutableList<ServiceAddDTO>
                    mBinding.recyclerView.visibility = View.VISIBLE
                    mBinding.emptyView.visibility = View.GONE
                } else {
                    mBinding.recyclerView.visibility = View.GONE
                    mBinding.emptyView.visibility = View.VISIBLE
                }
                mBinding.refreshLayout.finishRefresh()
            } else {
                if (taskList.isNotEmpty()) {
                    mAdapter?.addData(taskList)
                    mBinding.refreshLayout.finishLoadMore()
                } else {
                    mBinding.refreshLayout.finishLoadMoreWithNoMoreData()
                }
            }
        }

    }

    override fun initListener() {
        super.initListener()
        mBinding?.refreshLayout?.setOnRefreshLoadMoreListener(object : OnRefreshLoadMoreListener {
            override fun onLoadMore(@NonNull refreshLayout: RefreshLayout) {
                mViewModel.page += 1
                mViewModel.getPmsServiceList(key, limit = 10)
            }

            override fun onRefresh(@NonNull refreshLayout: RefreshLayout) {
                mViewModel.page = 1
                mViewModel.getPmsServiceList(key, limit = 10)
            }
        })


        mBinding.tvAdd.setOnClickListener {
            RouteManager.Mine.intentToTaskReleaseAdd()
        }

        mBinding.etKeyword.addTextChangedListener(object : TextWatcher {
            override fun onTextChanged(
                s: CharSequence,
                start: Int,
                before: Int,
                count: Int
            ) {

            }

            override fun beforeTextChanged(
                s: CharSequence,
                start: Int,
                count: Int,
                after: Int
            ) {

            }

            override fun afterTextChanged(s: Editable) {
                key=mBinding.etKeyword.text.toString()
            }
        })
    }

    override fun onClick(v: View?) {
        when (v?.id) {
            //添加服务
            R.id.tv_add ->RouteManager.Service.intentToServiceAdd(1, 0)


            //搜索
            R.id.tv_search->{
                mViewModel.page=1
                mViewModel.getPmsServiceList(key, limit = 10)
            }
        }
    }

}